#include "gd32f4xx.h"
#include "sys_type.h"
#include "bsp_iic.h"
#include "tmp112.h"


#if 0
void DELAY_2US(void)
{
	int delay_time = 10;
	while(delay_time--);
}


void iic_int(void)
{
    /* enable GPIOB clock */
    rcu_periph_clock_enable(RCU_GPIOB);
	gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6);
	gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
	
	I2C_SCL_L;
	DELAY_2US();
	I2C_SDA_L;
	DELAY_2US();
	
	I2C_SCL_H;
	DELAY_2US();
	I2C_SDA_L ;
	DELAY_2US();
	 I2C_SDA_H;
	DELAY_2US();
}

 
 
//IIC  产生起始信号
void I2C_StarT(void)
{
 
    I2C_SDA_H;
    I2C_SCL_H;
	DELAY_2US();
    I2C_SDA_L;
	DELAY_2US();
    I2C_SCL_L;
}
 
 
//IIC 产生停止信号
void I2C_StoP(void)
{
 
    I2C_SCL_L;
    I2C_SDA_L;
    I2C_SCL_H;
	DELAY_2US();
    I2C_SDA_H;
	DELAY_2US();
}
 
//IIC 主机产生应答信号ACK
void I2C_Ack(void)
{
    I2C_SCL_L;
    I2C_SDA_L;
	DELAY_2US();
    I2C_SCL_H;
	DELAY_2US();
    I2C_SCL_L;
}
 
 
 
//IIC 主机不产生应答信号NACK
void I2C_NAck(void)
{
    I2C_SCL_L;
    I2C_SDA_H;
	DELAY_2US();
    I2C_SCL_H;
	DELAY_2US();
    I2C_SCL_L;
}
 
 
 
//IIC 等待从机应答信号
//返回值：1 接收应答失败
//		  0 接收应答成功
u8 I2C_Wait_Ack(void)
{
    u8 tempTime = 0;
    
    I2C_SDA_H;
	DELAY_2US();
    I2C_SCL_H;
	DELAY_2US();
 
    while(gpio_input_bit_get(GPIO_I2C, I2C_SDA))
    {
        tempTime++;
		DELAY_2US();
        if(tempTime > 250)
        {
            I2C_StoP();
            return 1;
        }
    }
 
    I2C_SCL_L;
	DELAY_2US();
    return 0;
}
 
 
//I2C 发送一个字节
void I2C_Send_Byte(u8 txd)
{
    u8 i = 0;
    I2C_SCL_L;//拉低时钟开始数据传输
    for(i = 0; i < 8; i++)
    {
        if((txd & 0x80) > 0) //0x80  1000 0000
            I2C_SDA_H;
        else
            I2C_SDA_L;
        txd <<= 1;
		DELAY_2US();
        I2C_SCL_H;
		DELAY_2US();
        I2C_SCL_L;
		DELAY_2US();
    }
}
 
 
//I2C 读取一个字节
u8 I2C_Read_Byte(u8 ack)
{
    u8 i = 0, receive = 0;
    for(i = 0; i < 8; i++)
    {
        I2C_SCL_L;
		DELAY_2US();
        I2C_SCL_H;
        //while(!GPIO_ReadInputDataBit(GPIO_I2C,I2C_SCL));
        receive <<= 1;
        if(gpio_input_bit_get(GPIO_I2C, I2C_SDA))
            receive++;
		DELAY_2US();
    }
    if(ack == 0)
        I2C_NAck();
    else
        I2C_Ack();
    return receive;
}

float vTMP112_Temp(u8 addr)
{
    short tem, hum;
    u8 buff[6];
    float Temperature = 0;
 
    I2C_StarT();
    I2C_Send_Byte(addr << 1 | 0);    //写7位I2C设备地址加0作为写取位,1为读取位
    I2C_Wait_Ack();
    I2C_Send_Byte(0x00);
    I2C_Wait_Ack();
    
	DELAY_2US();
 
    I2C_StarT();
    I2C_Send_Byte(addr << 1 | 1);    //写7位I2C设备地址加0作为写取位,1为读取位
    if(I2C_Wait_Ack() == 0)
    {
        buff[0] = I2C_Read_Byte(1);
        buff[1] = I2C_Read_Byte(0);
 
        I2C_StoP();
 
    }
    else
    {
        buff[0] = 0;
        buff[1] = 0;
        I2C_StoP();
    }
    if(buff[0] & 0x80)
    {
        tem = (buff[0] << 8) | buff[1] ;
        hum = (((~tem) >> 4) + 1) & 0x0fff;
        Temperature = -(float)((float)hum * 0.0625);
    }
    else
    {
        tem = (buff[0] << 8) | buff[1] ;
        hum = tem >> 4;
        Temperature = (float)((float)hum * 0.0625);
    }
 
	return Temperature;
}

#endif

float vTMP112_Temp(void)
{
	float wk_temp;
	short tem,hum;
	UCHAR wk_reg[2] = {0};

	I2C_Mem_Read(I2C0,(DEVICE_ADDR << 1),&wk_reg[0],0x00, 2);
    if(wk_reg[0] & 0x80)
    {
        tem = (wk_reg[0] << 8) | wk_reg[1] ;
        hum = (((~tem) >> 4) + 1) & 0x0fff;
        wk_temp = -(float)((float)hum * 0.0625);
    }
    else
    {
        tem = (wk_reg[0] << 8) | wk_reg[1] ;
        hum = tem >> 4;
        wk_temp = (float)((float)hum * 0.0625);
    }
	return wk_temp;
}
